package com.example.leo.mycharmingstudent.a0923;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class MyContentProvider extends ContentProvider {

    private static final String TAG = "MyContentProvider";

    private SQLiteDatabase mDatabase;

    /**
     * MyContentProvider的authorities属性值
     */
    public static final String CONTENT_PROVIDER_AUTHORITY = "com.yinggu.mycharmingstudent";

    public static final String TABLE_NAME = "info_tb";
    private UriMatcher mUriMatcher;

    public MyContentProvider() {
    }

    @Override
    public boolean onCreate() {
        SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper(getContext(), "stu.db", null, 1) {

            @Override
            public void onCreate(SQLiteDatabase db) {
                String sql = "create table info_tb (_id integer primary key autoincrement," +
                        "name varchar(20)," +
                        "age integer," +
                        "gender varchar(20))";
                db.execSQL(sql);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            }
        };
        mDatabase = sqLiteOpenHelper.getReadableDatabase();

        /*
        UriMatcher的作用是匹配URI
        UriMatcher.NO_MATCH表示未匹配到URI的识别码
        识别码（addURI第三个参数）：匹配到以后返回的数值
         */
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(CONTENT_PROVIDER_AUTHORITY, "helloworld", 1000);
        mUriMatcher.addURI(CONTENT_PROVIDER_AUTHORITY, "helloworld/abc", 1001);
        //#表示匹配任意数字
        mUriMatcher.addURI(CONTENT_PROVIDER_AUTHORITY, "helloworld/#", 1002);
        //*表示匹配任意字符
        mUriMatcher.addURI(CONTENT_PROVIDER_AUTHORITY, "helloworld/*", 1003);

        //注意，此处需要return true。
        return true;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        long id = -1;
        if (values.size() > 0) {
            id = mDatabase.insert(TABLE_NAME, null, values);
        } else {
            //获取唯一标识
            String authority = uri.getAuthority();
            //获取路径
            String path = uri.getPath();
            //获取所有传来的数据
            String query = uri.getQuery();
            String formatData = String.format("authority:%s--\npath:%s--\nquery:%s--", authority, path, query);
            Log.e(TAG,formatData);
            String name = uri.getQueryParameter("name");
            String age = uri.getQueryParameter("age");
            String gender = uri.getQueryParameter("gender");
            ContentValues newValues = new ContentValues();
            newValues.put("name",name);
            newValues.put("age",age);
            newValues.put("gender",gender);
            id = mDatabase.insert(TABLE_NAME,null,newValues);
        }

        return ContentUris.withAppendedId(uri, id);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int result = -1;
        int code = mUriMatcher.match(uri);
        switch (code) {
            case 1000:
                Log.e(TAG, "匹配到helloworld路径"+1000);
                break;
            case 1001:
                Log.e(TAG, "匹配到helloworld/abc路径"+1001);
                break;
            case 1002:
                Log.e(TAG, "匹配到helloworld/任意数字路径"+1002);
                break;
            case 1003:
                Log.e(TAG, "匹配到helloworld/任意字符路径"+1003);
                break;
            default:
                //注意：selection和selectionArgs是null，会删除所有数据
                result = mDatabase.delete(TABLE_NAME, selection, selectionArgs);
                break;
        }
        return result;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int result = mDatabase.update(TABLE_NAME, values, selection, selectionArgs);
        return result;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        Cursor cursor = mDatabase.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
        return cursor;
    }


    /**
     * 默认就好
     * 返回MIME类型
     *
     * @param uri
     * @return
     */
    @Override
    public String getType(Uri uri) {
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
